package pct.page;

import java.util.LinkedList;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import pct.database.PcServiceImpl;
import pct.entity.Group;
import pct.entity.Post;
import pct.entity.Topic;
import pct.manage.StatusManager;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("serial")
public class LastPage extends ActionSupport implements Preparable, SessionAware {

	private PcServiceImpl pcService = new PcServiceImpl();
	private Map<String, Object> session;
	private String type;
	private String target;
	
	public String getTarget() {
		return target;
	}
	public void setTarget(String target) {
		this.target = target;
	}
	public String execute() {
		if(type.equals("1"))
		{
			StatusManager sm = (StatusManager) session.get("status");
			PageFilter pf = new PageFilter();
			sm.setPageSize(pf.getGroupPages(sm.getCurrentUser()));
			
			switch(sm.getPageType()) {
			case 1:
				// viewing groups
				int pageSize = pf.getGroupPages(sm.getCurrentUser());
				LinkedList<Group> relatedGroups = pf.getLastPageGroups(sm.getCurrentUser());
				if (relatedGroups != null) {
					for (Group i : relatedGroups) {
						i.setTopicNumber(pcService.retrieveTopicNumber(i
								.getGroupName()));
						i.setPostNumber(pcService.retrievePostNumber(i
								.getGroupName()));
					}
				}
				sm.setRelatedGroups(relatedGroups);
				sm.setCurrentPage(pageSize);
				break;
			case 2:
				// viewing topics
				int pageSize_2 = pf.getTopicPages(sm.getCurrentGroupName());
				LinkedList<Topic> relatedTopics = pf.getLastPageTopics(sm.getCurrentGroupName());
				if(relatedTopics!=null)
				{
					for(Topic i : relatedTopics) {
						i.setPostNumber(pcService.retrievePostNumber(i.getTopicID()));
					}
				}
				sm.setRelatedTopics(relatedTopics);
				sm.setCurrentPage(pageSize_2);
				break;
			case 3:
				break;
			case 11:
				break;
			case 12:
				// viewing topics by search
				
				break;
			case 13:
				break;
			}
			session.put("status", sm);
			return "home";
		}else if(type.equals("2"))
		{
			StatusManager sm = (StatusManager) session.get("status");
			PageFilter pf = new PageFilter();
			sm.setPageSize(pf.getSearchGroupPages(target));
			
			switch(sm.getPageType()) {
			case 4:
				// viewing groups
				int pageSize = pf.getSearchGroupPages(target);
				LinkedList<Group> relatedGroups = pf.getLastPageMatchedGroups(target);
				if (relatedGroups != null) {
					for (Group i : relatedGroups) {
						i.setTopicNumber(pcService.retrieveTopicNumber(i
								.getGroupName()));
						i.setPostNumber(pcService.retrievePostNumber(i
								.getGroupName()));
					}
				}
				sm.setSearchedGroups(relatedGroups);
				sm.setCurrentPage(pageSize);
				break;
			case 5:
				// viewing topics
				int pageSize_2 = pf.getSearchTopicPages(target);
				LinkedList<Topic> relatedTopics = pf.getLastPageMatchedTopics(target);
				if(relatedTopics!=null)
				{
					for(Topic i : relatedTopics) {
						i.setPostNumber(pcService.retrievePostNumber(i.getTopicID()));
					}
				}
				sm.setSearchedTopics(relatedTopics);
				sm.setCurrentPage(pageSize_2);
				break;
			case 6:
				int pageSize_3 = pf.getSearchPostPages(target);
				LinkedList<Post> relatedPosts = pf.getLastPageMatchedPosts(target);
				if(relatedPosts!=null)
				{
					for(Post i : relatedPosts) {
						i.setUserID(i.getUserID());
						i.setPostTime(i.getPostTime());
						i.setGroupName(pcService.retrieveGroupName(i.getTopicID()));
						i.setTitle(pcService.retrieveTitle(i.getTopicID()));
					}
				}
				sm.setSearchedPosts(relatedPosts);
				sm.setCurrentPage(pageSize_3);
				break;
			case 11:
				break;
			case 12:
				// viewing topics by search
				
				break;
			case 13:
				break;
			}
			session.put("status", sm);
			return "search";
		}else
			return SUCCESS;
	}
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void prepare() throws Exception {
		pcService.connectDB();
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
}
